package com.yxh.lib_base.util.permissions

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ToastUtils
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.XXPermissions

/**
 * @作者：yuan_xh
 * @时间：2022/8/26 20:04
 * @描述： 权限工具了
 **/
object PermissionsUtils {

    private fun getXXPermissions(current: Any) = kotlin.run {
        when (current) {
            is Fragment -> XXPermissions.with(current.requireContext())
            is Activity -> XXPermissions.with(current)
            is Context -> XXPermissions.with(current)
            else -> {
                throw ClassCastException("不支持的类型")
            }
        }
    }


    fun requestMultiple(
        current: Any,
        permissions: MutableList<String>.() -> Unit,
        onGranted: () -> Unit,
        isShowDialog: Boolean = true
    ) {
        val list = mutableListOf<String>()
        permissions.invoke(list)

        if (list.isEmpty()) {
            onGranted.invoke()
            return
        }

        getXXPermissions(current).permission(list).request(object : OnPermissionCallback {

            override fun onGranted(permissions: MutableList<String>, all: Boolean) {
                if (all){
                    onGranted.invoke()
                }
            }

            override fun onDenied(permissions: MutableList<String>, never: Boolean) {
                if (never) {
//                        toast("被永久拒绝授权，请手动授予录音和日历权限")
                    // 如果是被永久拒绝就跳转到应用权限系统设置页面
                    startPermissionActivity(current, permissions)
                } else {
                    ToastUtils.showLong("获取权限失败")
                }
                
                if (permissions.isNotEmpty()){

                }

            }
        })

    }


    fun requestPermissions(
        current: Any, permission: XXPermissions.() -> Unit,
        onDenied: ((MutableList<String>, Boolean) -> Unit)? = null,
        onGranted: (MutableList<String>, Boolean) -> Unit
    ) {
        var xxPermissions = getXXPermissions(current)
        permission.invoke(xxPermissions)
        // 设置权限请求拦截器（局部设置）
        //.interceptor(new PermissionInterceptor())
        // 设置不触发错误检测机制（局部设置）
        //.unchecked()
        xxPermissions.request(object : OnPermissionCallback {

            override fun onGranted(permissions: MutableList<String>, all: Boolean) {
                onGranted.invoke(permissions, all)
//                if (all) {
//                    toast("获取部分权限成功，但部分权限未正常授予")
//                    return
//                }
//                toast("获取录音和日历权限成功")
            }

            override fun onDenied(permissions: MutableList<String>, never: Boolean) {
                if (never) {
//                        toast("被永久拒绝授权，请手动授予录音和日历权限")
                    // 如果是被永久拒绝就跳转到应用权限系统设置页面
                    startPermissionActivity(current, permissions)
                } else {
                    ToastUtils.showLong("获取权限失败")
                }
            }
        })
    }


    private fun startPermissionActivity(current: Any, permissions: MutableList<String>) {
        // 如果是被永久拒绝就跳转到应用权限系统设置页面
        when (current) {
            is Fragment -> XXPermissions.startPermissionActivity(current.activity, permissions)
            is android.app.Fragment -> XXPermissions.startPermissionActivity(current, permissions)
            is Activity -> XXPermissions.startPermissionActivity(current, permissions)
            is Context -> XXPermissions.startPermissionActivity(current, permissions)
            else -> {
                throw ClassCastException("不支持的类型")
            }
        }
    }
}